package fuck.livenessdetectionxposed;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;

import java.io.ByteArrayOutputStream;
import java.io.File;

import de.robv.android.xposed.XposedBridge;

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_CROP = 101;
    private static final int REQUEST_CODE_GET = 102;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, REQUEST_CODE_GET);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_CODE_GET) {
                File file = new File(Environment.getExternalStorageDirectory(), "/crop.png");
                Uri uri = data.getData();
                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setDataAndType(uri, "image/*");
                // 设置裁剪
                intent.putExtra("crop", "true");
                // aspectX aspectY 是宽高的比例
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                // outputX outputY 是裁剪图片宽高
                intent.putExtra("outputX", 305);
                intent.putExtra("outputY", 305);
                intent.putExtra("return-data", false);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(intent, REQUEST_CODE_CROP);
            } else if (requestCode == REQUEST_CODE_CROP) {
                File file = new File(Environment.getExternalStorageDirectory(), "/crop.png");
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                if (bitmap.getWidth() != 305) {
                    Bitmap nBitmap = corpBitmap(bitmap, 305, 305);
                    if (!nBitmap.equals(bitmap)) {
                        bitmap.recycle();
                    }
                    bitmap = nBitmap;
                }
                bitmap2Bytes(bitmap, 70);
            }
        }
    }

    /**
     * 用尽可能低的内存消耗裁剪获取指定大小的bitmap。
     * <p/>
     * 不保持原始图片的宽高比，因此确保返回bitmap的尺寸与设定的相同。
     * <p/>
     */
    public static Bitmap corpBitmap(Bitmap tempBitmap, int reqWidth, int reqHeight) {
        Bitmap resizeBmp;
        Matrix matrix = new Matrix();
        float scaleWidth = (float) reqWidth / (float) tempBitmap.getWidth();
        float scaleHeight = (float) reqHeight / (float) tempBitmap.getHeight();
        matrix.postScale(scaleWidth, scaleHeight);
        resizeBmp = Bitmap.createBitmap(tempBitmap, 0, 0, tempBitmap.getWidth(), tempBitmap.getHeight(), matrix, true);
        return resizeBmp;
    }

    public static byte[] bitmap2Bytes(Bitmap bitmap, int i) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, i, byteArrayOutputStream);
            byte[] toByteArray = byteArrayOutputStream.toByteArray();
            byteArrayOutputStream.flush();
            byteArrayOutputStream.close();
            return toByteArray;
        } catch (Throwable e) {
            XposedBridge.log(e);
            return null;
        }
    }
}
